registerNS('com.cipe.cmrs.model');

com.cipe.cmrs.model.PaymentLineItem = function(_containerId, _arr_projects, _account_codes, _arr_lineItems, _tabIndex, _totalAmountListener) {
	var PREFIX = "payment-line-item-";
	var THEAD_ID = PREFIX + "thead";
	var TBODY_ID = PREFIX + "tbody";
	var TABLE_CLASS = PREFIX + "table-class";
	var INPUT_PAYMENT_ID = PREFIX + 'payment-id';
	var INPUT_LINE_NO = PREFIX + 'line-no';
	var INPUT_AMOUNT = PREFIX + 'amount';
	var INPUT_ACCOUNT_CODE = PREFIX + 'account-code';
	var INPUT_ACCOUNT_DESC = PREFIX + 'account-desc';
//	var SELECT_PROJECTID_DIV = PREFIX + 'hidden-div-';
	var INPUT_PROJECT_ID = PREFIX + 'project-id';
//	var HIDDEN_PROJECT_ID = PREFIX + 'hidden-project-id';
	var INPUT_PROJECT_TYPE = PREFIX + 'project-type';
	var INPUT_PROJECT_STATUS = PREFIX + 'project-status';
	var INPUT_PROJECT_NAME = PREFIX + 'project-name';
	var INPUT_PROJECT_ACRONYM = PREFIX + 'project-acronym';
	var INPUT_PROJECT_TITLE = PREFIX + 'project-title';
	var INPUT_PROJECT_REGION = PREFIX + 'project-region';
	var INPUT_PROJECT_COUNTRY = PREFIX + 'project-country';
	var INPUT_PROJECT_START_DATE = PREFIX + 'project-start-date';
	var INPUT_PROJECT_END_DATE = PREFIX + 'project-end-date';
	var INPUT_PROJECT_FUNDS = PREFIX + 'project-funds';
	var LINE_ITEM_ENTRY = PREFIX + 'line-item';
	
	var containerId = _containerId;
	var arr_projects = _arr_projects;
	var account_codes = _account_codes;
	var arr_lineItems = _arr_lineItems;
	var totalAmountListener = _totalAmountListener;
	var tabIndex = _tabIndex;
	var tempId = -120;
	var prevProjectType = '';
	var prevProjectId = '';
	var prevAccountId = '';
	
	this.has6570or6571AccountCode = function() {
		var result = false;
		var accountCodes = $('select[class^="' + INPUT_ACCOUNT_CODE + '"]');
		for(var i = 0; i < accountCodes.length; i++) {
			var obj = getAccountCodeById(accountCodes[i].value);
			if(obj && (obj.code == '6570' || obj.code == '6571')) {
				result = true;
			}
		}
		return result;
	}
	
	function isFloat(s) {
		return !isEmpty(s) && reFloat.test(s);
	}
	
	function rowModified(element) {
		if(totalAmountListener != null) {
			var amountFields = $('input[class^="' + INPUT_AMOUNT + '"]');
			var totalAmount = 0.0;
			for(var i = 0; i < amountFields.length; i++) {
				var index = parseInt(amountFields[i].className.substring(INPUT_AMOUNT.length));
				var accountCode = $('.' + INPUT_ACCOUNT_CODE + index).val();
				var projectId = $('.' + INPUT_PROJECT_ID + index).val();
				var amount = amountFields[i].value;
				var fAmount = parseFloat(amount);
				if( fAmount && "" != accountCode && "" != projectId) {
					totalAmount += parseFloat(amount);
				}
			}
			totalAmountListener(totalAmount);
		}
		var tr = $(element).closest('tr');
		var amount = tr.find('td.' + INPUT_AMOUNT + ' input').val();
		var accountCode = tr.find('td.' + INPUT_ACCOUNT_CODE + ' select').val();
		var projectId =  tr.find('select[class^="' + INPUT_PROJECT_ID + '"]').val();
		var fAmount = parseFloat(amount);
		
		var lineItemId = tr.find('td.id-column input.' + INPUT_PAYMENT_ID).val();
		
		var lineItem = getLineItemById(lineItemId);
		// XXX: This should never be null. check createRow2. but for some reason it is :(... Allan
		if(lineItem != null) {
			if( fAmount && "" != accountCode && "" != projectId) {
				lineItem.amount = fAmount;
			} else if(lineItem != null) {
				lineItem.amount = 0;
			}
			
			var prevProjId = lineItem.cipeProjectId;
			lineItem.cipeProjectId = projectId;
			lineItem.accountCode = accountCode; // This is not needed. But it do no harm :)
			
			// Update previous value
			if(prevProjId != "" && prevProjId != projectId) {
				updateAffectedProjects(prevProjId);
			}
			// Update current projects
			updateAffectedProjects(projectId);
		}
	}
	
	function updateAffectedProjects(projectId) {
		if("" != projectId) {
			var totalAmount = getTotalAmount(projectId);
			var project = getProjectByProjectId(projectId)
			if(project != null) {
				// Update the Remaining Funds
				var affectedProjects = $('td.' + INPUT_PROJECT_ID + ' option[value="' + projectId + '"]:selected');
				if(affectedProjects.length > 0) {
					// Update fund cells
					for(var i = 0; i < affectedProjects.length; i++) {
						var fundCell =  $(affectedProjects[i]).closest('tr').find('td.' + INPUT_PROJECT_FUNDS + ' span');
						fundCell.text(project.remainingFunds - totalAmount);
						$(fundCell).formatCurrency({roundToDecimalPlace:0});
					}
				}
			}
		}
	}
	
	function getTotalAmount(projectId) {
		var totalAmount = 0;
		if(arr_lineItems != null) {
			for(var i = 0; i < arr_lineItems.length; i++) {
				if(arr_lineItems[i].cipeProjectId == projectId) {
					totalAmount += arr_lineItems[i].amount;
				}
			}
		}
		return totalAmount;
	}
	
	init();
	
	function init() {
		var htmlString = '';
		htmlString += '<div>';
		htmlString += '<table class="' + TABLE_CLASS + '">';
		htmlString += '	<thead id="' + THEAD_ID + '">';
		htmlString += '		<tr>';
		htmlString += '			<th width="20">&nbsp;</th>';
		htmlString += '			<th width="45">Line #</th>';
		htmlString += '			<th width="45" class="emphasized_col">Account</th>';
		htmlString += '			<th width="100">Account Code Description</th>';
		htmlString += '			<th width="70" class="emphasized_col">Amount</th>';
		htmlString += '			<th width="150" class="emphasized_col">Project ID</th>';
		htmlString += '			<th width="50">Available Funds</th>';
		htmlString += '			<th width="45">Project Type</th>';
		htmlString += '			<th width="45">Status</th>';
		htmlString += '			<th width="150">Partner Name</th>';
		htmlString += '			<th width="80">Acronym</th>';
		htmlString += '			<th width="200">Title</th>';
		htmlString += '			<th width="45">Region</th>';
		htmlString += '			<th width="45">Country</th>';
		htmlString += '			<th width="50">Start Date</th>';
		htmlString += '			<th width="50">End Date</th>';
		htmlString += '		</tr>';
		htmlString += '	</thead>';
		htmlString += '	<tbody id="' + TBODY_ID + '">';
		htmlString += '	</tbody>';
		htmlString += '</table>';
		htmlString += '</div>';
		htmlString += '<div>';
		htmlString += '</div>';
		$('#' + containerId).html(htmlString);

		$('.' + TABLE_CLASS).flexigrid({
			height:'auto',
			nowrap: true,
			showToggleBtn: false,
			buttons : 	[ 
			          	  {
							name: 'Add', bclass: 'add', onpress : function() 
							{
								var rowString = createRow2($('#' + TBODY_ID + " tr").length, null);
								$('#' + TBODY_ID).append(rowString);
							}
						  },
			          	  {
							name: 'Delete', bclass: 'delete', onpress : function()
							{
								var checked = $('td.id-column input:checked');
								if(checked.length > 0) {
									for(var i = 0; i < checked.length; i++) {
										var tr = $(checked[i]).closest('tr');
										var lineItemId = tr.find('td.id-column input.' + INPUT_PAYMENT_ID).val();
										var lineItem = getLineItemById(lineItemId);
										// XXX: This should never be null. check createRow2. but for some reason it is :(... Allan
										if(lineItem != null) {
											lineItem.amount = 0;
											
											var prevProjId = lineItem.cipeProjectId;
											lineItem.cipeProjectId = '';
											lineItem.accountCode = ''; // This is not needed. But it do no harm :)
											
											// Update previous value
											if(prevProjId != "") {
												updateAffectedProjects(prevProjId);
											}
										}
										
										tr.find('td.' + INPUT_AMOUNT + ' input[class^="' + INPUT_AMOUNT + '"]').val('0.0');
										tr.find('td.' + INPUT_AMOUNT + ' input[class^="formatted-' + INPUT_AMOUNT + '"]').val('$0.00');
										tr.find('td.' + INPUT_ACCOUNT_CODE + ' select').val('');
										tr.find('td.' + INPUT_PROJECT_ID + ' select').val('');
										var spans = tr.find('td span');
										if(spans.length > 0) {
											for(var j = 0; j < spans.length; j++) {
												$(spans[j]).text('');
											}
										}
										
										if(totalAmountListener != null) {
											var amountFields = $('input[class^="' + INPUT_AMOUNT + '"]');
											var totalAmount = 0.0;
											for(var i = 0; i < amountFields.length; i++) {
												var index = parseInt(amountFields[i].className.substring(INPUT_AMOUNT.length));
												var accountCode = $('.' + INPUT_ACCOUNT_CODE + index).val();
												var projectId = $('.' + INPUT_PROJECT_ID + index).val();
												var amount = amountFields[i].value;
												var fAmount = parseFloat(amount);
												if( fAmount && "" != accountCode && "" != projectId) {
													totalAmount += parseFloat(amount);
												}
											}
											totalAmountListener(totalAmount);
										}
									}
								}
							}
						  }
			          	  /*, {separator: true}*/ 
						],
			title: 'Payment Line Items',
		});
		
		repopulateData();
		
		$('select[class^="' + INPUT_ACCOUNT_CODE + '"]').live('focus', function() {
			prevAccountId = this.value;
		});
		
		$('select[class^="' + INPUT_ACCOUNT_CODE + '"]').live('change', function() {
			var tr = $(this).closest('tr');
			var index = parseInt(this.className.substring(INPUT_ACCOUNT_CODE.length));
			var accountCode = null;
			var code = -1;
			if(this.value && this.value != "") {
				accountCode = getAccountCodeById(this.value);
				code = parseInt(accountCode.code);
			}
			
			if(code == 6790 && 'Partner Payment' != $('#paymentTypeId :selected').text()) {
				$(this).val(prevAccountId);
				alert('Payment request type for account code 6790 must be "Partner Payment".');
				return;
			}
			
			prevAccountId = this.value;
			tr.find('td span.' + INPUT_ACCOUNT_DESC + index + ' input').val(accountCode != null ? accountCode.description : "");
			rowModified(this);
			
			if(code >= 6740 && code <= 6749) {
				alert("A 'Who/What/Where/When/Why' Memo is required and needs to be attached to this payment request!");
			}
		});
		$('select[class^="' + INPUT_ACCOUNT_CODE + '"]').change();
		
		$('select[class^="' + INPUT_PROJECT_ID + '"]').live('focus', function() {
			var tr = $(this).closest('tr');
			var index = parseInt(this.className.substring(INPUT_PROJECT_ID.length));
			prevProjectType = tr.find('td span.' + INPUT_PROJECT_TYPE + index + ' input').val();
			prevProjectId = this.value;
		});
		
		$('select[class^="' + INPUT_PROJECT_ID + '"]').live('change', function() {
			var tr = $(this).closest('tr');
			var index = parseInt(this.className.substring(INPUT_PROJECT_ID.length));
			var project = null;
			if(this.value && this.value != "") {
				project = getProjectByProjectId(this.value);
			}
			
			tr.find('td span.' + INPUT_PROJECT_STATUS + index + ' input').val(project != null ? project.status : "");
			tr.find('td span.' + INPUT_PROJECT_TYPE + index + ' input').val(project != null ? project.projectType : "");
			tr.find('td span.' + INPUT_PROJECT_NAME + index + ' input').val(project != null ? project.partnerName : "");
			tr.find('td span.' + INPUT_PROJECT_ACRONYM + index + ' input').val(project != null ? project.acronym : "");
			tr.find('td span.' + INPUT_PROJECT_TITLE + index + ' input').val(project != null ? project.title : "");
			tr.find('td span.' + INPUT_PROJECT_REGION + index + ' input').val(project != null ? project.region : "");
			tr.find('td span.' + INPUT_PROJECT_COUNTRY + index + ' input').val(project != null ? project.country : "");
			tr.find('td span.' + INPUT_PROJECT_START_DATE + index + ' input').val(project != null ? project.formattedStartDate : "");
			tr.find('td span.' + INPUT_PROJECT_END_DATE + index + ' input').val(project != null ? project.formattedEndDate : "");
			var fundCell = tr.find('td span.' + INPUT_PROJECT_FUNDS + index);
			fundCell.text(project != null ? project.remainingFunds : "");

			$(fundCell).formatCurrency({roundToDecimalPlace:0});
			
			var currProjectType = project != null ? project.projectType : '';
			if(currProjectType != prevProjectType) {
				var accountCodeId = tr.find('td select.' + INPUT_ACCOUNT_CODE + index).val();
				
				var heads = $('#' + THEAD_ID + ' th div');
				var htmlString = '';
				htmlString += '<div style="width: ' + $(heads[2]).css('width') + '; white-space: normal;">';
				htmlString += createAccountCodeTd(index, INPUT_ACCOUNT_CODE, 'lineItemBeans[' + index + '].accountCodeId', accountCodeId, currProjectType );
				htmlString += '</div>';
				tr.find('td.' + INPUT_ACCOUNT_CODE).html(htmlString);
				
				var ac = getAccountCodeById(accountCodeId);
				if(ac != null) {
					var accountCode = parseInt(ac.code);
					if(currProjectType != null && currProjectType != '' &&  
						(('Balance Sheet' == currProjectType && (accountCode < 1020 || accountCode > 1431)) 
							|| ('Balance Sheet' != currProjectType && (accountCode >= 1020 && accountCode <= 1431)))) {
						alert('The account code that you selected is not valid for this "' + currProjectType + '" type of project. Please select an account code that is valid for this project.');
					}					
				}
				prevProjectType = currProjectType;
			}
			rowModified(this);
		});
		$('select[class^="' + INPUT_PROJECT_ID + '"]').change();

		$('input[class^="' + INPUT_AMOUNT + '"]').live('blur', function() {
			var start = this.className.indexOf(INPUT_AMOUNT);
			var end = start + INPUT_AMOUNT.length + 1;
			var tobesubClassName = this.className.substring(start, end);
			var subClassName = 'formatted-' + tobesubClassName;
			
			$(this).formatCurrency('.' + subClassName,{colorize: true, negativeFormat: '(%s%n)'});
			
			showSubstitute(true, subClassName, tobesubClassName);
			rowModified(this);
		});
		
		$('input[class^="formatted-' + INPUT_AMOUNT + '"]').live('focus', function() {
			var start = this.className.indexOf(INPUT_AMOUNT);
			var end = start + INPUT_AMOUNT.length + 1;
			var tobesubClassName = this.className.substring(start, end);
			var subClassName = 'formatted-' + tobesubClassName;
			showSubstitute(false, subClassName, tobesubClassName);
		});
		
		$('input[class^="' + INPUT_AMOUNT + '"]').blur();
	}
	
	function updateProjectRowElements(cipeProjectId, index) {
		var project = getProjectByProjectId(cipeProjectId);
		if(project != null) {
			var tr = $('#' + LINE_ITEM_ENTRY + index);
			
			var projectIdInput = tr.find('td input.' + INPUT_PROJECT_ID + index);
			projectIdInput.val(project != null ? project.projectId : "");
			
			projectIdInput.next().val(project.id);
			
			tr.find('td span.' + INPUT_PROJECT_STATUS + index + ' input').val(project != null ? project.status : "");
			tr.find('td span.' + INPUT_PROJECT_TYPE + index + ' input').val(project != null ? project.projectType : "");
			tr.find('td span.' + INPUT_PROJECT_NAME + index + ' input').val(project != null ? project.partnerName : "");
			tr.find('td span.' + INPUT_PROJECT_ACRONYM + index + ' input').val(project != null ? project.acronym : "");
			tr.find('td span.' + INPUT_PROJECT_TITLE + index + ' input').val(project != null ? project.title : "");
			tr.find('td span.' + INPUT_PROJECT_REGION + index + ' input').val(project != null ? project.region : "");
			tr.find('td span.' + INPUT_PROJECT_COUNTRY + index + ' input').val(project != null ? project.country : "");
			tr.find('td span.' + INPUT_PROJECT_START_DATE + index + ' input').val(project != null ? project.formattedStartDate : "");
			tr.find('td span.' + INPUT_PROJECT_END_DATE + index + ' input').val(project != null ? project.formattedEndDate : "");
			var fundCell = tr.find('td span.' + INPUT_PROJECT_FUNDS + index);
			fundCell.text(project != null ? project.remainingFunds : "");
			$(fundCell).formatCurrency({roundToDecimalPlace:0});
			
			rowModified(projectIdInput);
		}
	}
	
	function getValue(val) {
		return val != undefined && val != null ? val : '&nbsp;';
	}
	
	function showSubstitute(show, subClassName, tobesubClassName) {
		if(show) {
			$('.' + subClassName).css('display', 'block');
			$('.' + tobesubClassName).css('display', 'none');
		} else {
			$('.' + subClassName).css('display', 'none');
			$('.' + tobesubClassName).css('display', 'block');
			$('.' + tobesubClassName).focus();
			$('.' + tobesubClassName).select();
		}
	}
	
	function repopulateData() {
		var htmlString = '';
		var max_row = arr_lineItems.length > 5 ? arr_lineItems.length : 5;
		for(var i = 0; i < max_row; i++) {
			var item = null;
			if(i < arr_lineItems.length ) {
				item = arr_lineItems[i];
			}
			htmlString += createRow2(i, item);
		}
		$('#' + TBODY_ID).append(htmlString);
		
		var projectIds = $('select[class^="' + INPUT_PROJECT_ID + '"]');
		for(var i = 0; i < projectIds.length; i++) {
			var index = parseInt(projectIds[i].className.substring(INPUT_PROJECT_ID.length));
			updateProjectRowElements(projectIds[i].value, index);
		};
		
	}
	
	function createRow2(index, item) {
		if(item == null) {
			tempId++;
			item = new Object();
			item.id = ("" + tempId);
			item.lineItemNumber = "";
			item.amount = 0.0;
			item.prevAmount = 0.0;
			item.accountCodeId = "";
			item.cipeProjectId = "";
			arr_lineItems[arr_lineItems.length] = item;
		}
		
		var heads = $('#' + THEAD_ID + ' th div');

		var htmlString = '';
		if(index%2 != 0) {
			htmlString += '<tr id="' + LINE_ITEM_ENTRY + index + '" class="erow">';
		} else {
			htmlString += '<tr id="' + LINE_ITEM_ENTRY + index + '">';
		}
		
		htmlString += '<td class="id-column">';
		htmlString += '<div style="width: ' + $(heads[0]).css('width') + '; white-space: normal;">';
		htmlString += createEmptyColumn(item.id, 'lineItemBeans[' + index + '].id');
		htmlString += '</div>';
		htmlString += '</td>';
		htmlString += '<td class="payment-line-item-line-no">';
		htmlString += '<div style="width: ' + $(heads[1]).css('width') + '; white-space: normal;">';
		htmlString += createSpanColumn(index, INPUT_LINE_NO, item.lineItemNumber);
		htmlString += '</div>';
		htmlString += '</td>';
		
		htmlString += '<td class="' + INPUT_ACCOUNT_CODE + '">';
		htmlString += '<div style="width: ' + $(heads[2]).css('width') + '; white-space: normal;">';
		
		var projectType = '';
		if(item.cipeProjectId != null && '' != item.cipeProjectId) {
			var project = getProjectByProjectId(item.cipeProjectId);
			if(project != null) {
				projectType = project.projectType;
			}
		}
		htmlString += createAccountCodeTd(index, INPUT_ACCOUNT_CODE, 'lineItemBeans[' + index + '].accountCodeId', item.accountCodeId, projectType );
		htmlString += '</div>';
		htmlString += '</td>';
		
		htmlString += '<td class="payment-line-item-account-desc">';
		htmlString += '<div style="width: ' + $(heads[3]).css('width') + '; white-space: normal;">';
		htmlString += createSpanColumn(index, INPUT_ACCOUNT_DESC)
		htmlString += '</div>';
		htmlString += '</td>';
		htmlString += '<td class="' + INPUT_AMOUNT + '">';
		htmlString += '<div style="width: ' + $(heads[4]).css('width') + '; white-space: normal;">';
		htmlString += createInputAmountColumn(index, INPUT_AMOUNT, 'lineItemBeans[' + index + '].amount', item.amount);
		htmlString += '</div>';
		htmlString += '</td>';
		htmlString += '<td class="' + INPUT_PROJECT_ID + '">';
		htmlString += '<div style="width: ' + $(heads[5]).css('width') + '; white-space: normal;">';
		htmlString += createProjectIdTd(index, INPUT_PROJECT_ID, 'lineItemBeans[' + index + '].cipeProjectId', item.cipeProjectId);
		htmlString += '</div>';
		htmlString += '</td>';
		htmlString += '<td class="' + INPUT_PROJECT_FUNDS + '">';
		htmlString += '<div style="width: ' + $(heads[6]).css('width') + '; white-space: normal;">';
		htmlString += createSpanColumn(index, INPUT_PROJECT_FUNDS);
		htmlString += '</div>';
		htmlString += '</td>';
		htmlString += '<td>';
		htmlString += '<div style="width: ' + $(heads[7]).css('width') + '; white-space: normal;">';
		htmlString += createSpanColumn(index, INPUT_PROJECT_TYPE);
		htmlString += '</div>';
		htmlString += '</td>';
		htmlString += '<td class="payment-line-item-project-status">';
		htmlString += '<div style="width: ' + $(heads[8]).css('width') + '; white-space: normal;">';
		htmlString += createSpanColumn(index, INPUT_PROJECT_STATUS);
		htmlString += '</div>';
		htmlString += '</td>';
		htmlString += '<td class="payment-line-item-project-name">';
		htmlString += '<div style="width: ' + $(heads[9]).css('width') + '; white-space: normal;">';
		htmlString += createSpanColumn(index, INPUT_PROJECT_NAME);
		htmlString += '</div>';
		htmlString += '</td>';
		htmlString += '<td class="payment-line-item-project-acronym">';
		htmlString += '<div style="width: ' + $(heads[10]).css('width') + '; white-space: normal;">';
		htmlString += createSpanColumn(index, INPUT_PROJECT_ACRONYM);
		htmlString += '</div>';
		htmlString += '</td>';
		htmlString += '<td class="payment-line-item-project-title">';
		htmlString += '<div style="width: ' + $(heads[11]).css('width') + '; white-space: normal;">';
		htmlString += createSpanColumn(index, INPUT_PROJECT_TITLE);
		htmlString += '</div>';
		htmlString += '</td>';
		htmlString += '<td>';
		htmlString += '<div style="width: ' + $(heads[12]).css('width') + '; white-space: normal;">';
		htmlString += createSpanColumn(index, INPUT_PROJECT_REGION);
		htmlString += '</div>';
		htmlString += '</td>';
		htmlString += '<td>';
		htmlString += '<div style="width: ' + $(heads[13]).css('width') + '; white-space: normal;">';
		htmlString += createSpanColumn(index, INPUT_PROJECT_COUNTRY);
		htmlString += '</div>';
		htmlString += '</td>';
		htmlString += '<td>';
		htmlString += '<div style="width: ' + $(heads[14]).css('width') + '; white-space: normal;">';
		htmlString += createSpanColumn(index, INPUT_PROJECT_START_DATE);
		htmlString += '</div>';
		htmlString += '</td>';
		htmlString += '<td>';
		htmlString += '<div style="width: ' + $(heads[15]).css('width') + '; white-space: normal;">';
		htmlString += createSpanColumn(index, INPUT_PROJECT_END_DATE);
		htmlString += '</div>';
		htmlString += '</td>';
		htmlString += '</tr>';
		return htmlString;
	}
	
	function createEmptyColumn(id, name) {
		var htmlString = '';
		htmlString += '<input type="hidden" class="' + INPUT_PAYMENT_ID + '" value="' + id + '" name="' + name + '"/>';
		htmlString += '<input type="checkbox" value="' + id + '" tabindex="-1"/>';
		return htmlString;
	}
	
	function createInputAmountColumn(index, classPrefix, name, value) {
		var htmlString = '';
		htmlString += '<input type="text" value="' + value + '" class="' + classPrefix + index + '" name="' + name + '" tabindex="-1" style="display: none"/>';
		htmlString += '<input type="text" class="formatted-' + classPrefix + index + '" value="$0.00" tabindex="' + getTabIndex() + '"/>';
		return htmlString;
	}
	
	function createSpanColumn(index, classPrefix, value) {
		if(value == null) {
			value = '';
		}
		var htmlString = '';
		htmlString += '<span class="' + classPrefix + index + '">';
		htmlString += '<input type="text" class="transparent_input uneditable" disabled="disabled" value="' + value + '"/>';
		htmlString += '</span>';
		return htmlString;
	}
	
	function createAccountCodeTd(index, classPrefix, name, value, projectType) {
		var htmlString = '';
		htmlString += '<select name="' + name + '" class="' + classPrefix + index + '" tabindex="' + getTabIndex() + '">';
		htmlString += '<option value="">Select</option>';
		for(var i = 0; i < account_codes.length; i++) {
			var accountCode = parseInt(account_codes[i].code);
			if(projectType != null && projectType != '' &&  
				(('Balance Sheet' == projectType && (accountCode < 1020 || accountCode > 1431)) 
					|| ('Balance Sheet' != projectType && (accountCode >= 1020 && accountCode <= 1431)))) {
				continue;
			}
			htmlString += '<option value="' + account_codes[i].id + '" ' + (account_codes[i].id == value ? 'selected="selected"' : '') + '>';
			htmlString += account_codes[i].displayName;
			htmlString += '</option>';
		}
		htmlString += '</select>';
		return htmlString;
	}
	
	function createProjectIdTd(index, classPrefix, name, value) {
		var htmlString = '';
		htmlString += '<select name="' + name + '" class="' + classPrefix + index + '" tabindex="' + getTabIndex() + '">';
		htmlString += '<option value="">Select</option>';
		for(var i = 0; i < arr_projects.length; i++) {
			htmlString += '<option value="' + arr_projects[i].id + '" ' + (arr_projects[i].id == value ? 'selected="selected"' : '') + '>';
			htmlString += arr_projects[i].displayName;
			htmlString += '</option>';
		}
		htmlString += '</select>';
		return htmlString;
	}
	
	function getProjectByProjectId(id) {
		if(arr_projects != null) {
			for(var i = 0; i < arr_projects.length; i++) {
				if(arr_projects[i].id == id) {
					return arr_projects[i];
				}
			}
		}
		return null;
	}
	
	function getAccountCodeById(id) {
		if(account_codes != null) {
			for(var i = 0; i < account_codes.length; i++) {
				if(account_codes[i].id == id) {
					return account_codes[i];
				}
			}
		}
		return null;
	}
	
	function getLineItemById(id) {
		if(arr_lineItems != null) {
			for(var i = 0; i < arr_lineItems.length; i++) {
				if(arr_lineItems[i].id == id) {
					return arr_lineItems[i];
				}
			}
		}
		return null;
	}
	
	function getTabIndex() {
		if(tabIndex != 'undefined') {
			return ++tabIndex;
		} else {
			return -1;
		}
	}
};